import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:go_router/go_router.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:craze/common/index.dart';
import 'dart:ui';

/// 登录提示弹窗
/// 在需要登录的功能处显示，提示用户登录或注册
class LoginDialog extends StatelessWidget {
  /// 弹窗标题
  final String title;

  /// 弹窗描述
  final String description;

  /// 返回路径
  final String returnPath;

  const LoginDialog({
    super.key,
    this.title = 'Authentication Required',
    this.description =
        'This feature requires authentication. Would you like to sign in now?',
    required this.returnPath,
  });

  @override
  Widget build(BuildContext context) {
    return Dialog(
      backgroundColor: Colors.transparent,
      elevation: 0,
      insetPadding: EdgeInsets.symmetric(horizontal: 24.w),
      child: ClipRRect(
        borderRadius: BorderRadius.circular(20.r),
        child: BackdropFilter(
          filter: ImageFilter.blur(sigmaX: 10, sigmaY: 10),
          child: Container(
            padding: EdgeInsets.all(20.w),
            decoration: BoxDecoration(
              color: MyColors.cardDark.withOpacity(0.8),
              borderRadius: BorderRadius.circular(20.r),
              border: Border.all(
                color: MyColors.white.withAlpha(50),
                width: 1.w,
              ),
              boxShadow: [
                BoxShadow(
                  color: Colors.black.withAlpha(30),
                  blurRadius: 10,
                  offset: const Offset(0, 5),
                ),
              ],
            ),
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: [
                // 标题
                Text(
                  title,
                  style: TextStyle(
                    color: MyColors.white,
                    fontSize: 20.sp,
                    fontWeight: FontWeight.bold,
                    fontFamily: GoogleFonts.mochiyPopOne().fontFamily,
                  ),
                ),

                SizedBox(height: 16.h),

                // 描述
                Text(
                  description,
                  textAlign: TextAlign.center,
                  style: TextStyle(
                    color: MyColors.white,
                    fontSize: 14.sp,
                    fontFamily: GoogleFonts.inter().fontFamily,
                  ),
                ),

                SizedBox(height: 24.h),

                // 按钮组
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                  children: [
                    // 取消按钮
                    Expanded(
                      child: GestureDetector(
                        onTap: () => Navigator.of(context).pop(),
                        child: Container(
                          height: 44.h,
                          decoration: BoxDecoration(
                            color: MyColors.cardDark,
                            borderRadius: BorderRadius.circular(22.r),
                            border: Border.all(
                              color: MyColors.white.withAlpha(50),
                              width: 1.w,
                            ),
                          ),
                          child: Center(
                            child: Text(
                              'Cancel',
                              style: TextStyle(
                                fontSize: 14.sp,
                                fontWeight: FontWeight.w500,
                                color: MyColors.white,
                                fontFamily:
                                    GoogleFonts.mochiyPopOne().fontFamily,
                              ),
                            ),
                          ),
                        ),
                      ),
                    ),

                    SizedBox(width: 16.w),

                    // 确定按钮
                    Expanded(
                      child: GestureDetector(
                        onTap: () {
                          Navigator.of(context).pop();
                          // 存储返回路径，登录成功后返回该页面
                          _navigateToAuthenticationPage(context, returnPath);
                        },
                        child: Container(
                          height: 44.h,
                          decoration: BoxDecoration(
                            color: MyColors.blue,
                            borderRadius: BorderRadius.circular(22.r),
                            border: Border.all(
                              color: MyColors.white.withAlpha(100),
                              width: 1.w,
                            ),
                          ),
                          child: Center(
                            child: Text(
                              'Sign In',
                              style: TextStyle(
                                fontSize: 14.sp,
                                fontWeight: FontWeight.w500,
                                color: MyColors.white,
                                fontFamily:
                                    GoogleFonts.mochiyPopOne().fontFamily,
                              ),
                            ),
                          ),
                        ),
                      ),
                    ),
                  ],
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }

  /// 导航到认证选择页面
  void _navigateToAuthenticationPage(BuildContext context, String returnPath) {
    // 使用path参数传递完整的返回路径，包括原始页面路径
    GoRouter.of(context).pushNamed(
      'choose',
      queryParameters: {
        'returnPath': returnPath,
        'fromPage': 'login_dialog',
        'preserveNavStack': 'true', // 添加标记以保留导航栈
      },
    );
  }

  /// 显示登录提示弹窗的静态方法
  static Future<bool?> show(
    BuildContext context, {
    String title = 'Authentication Required',
    String description =
        'This feature requires authentication. Would you like to sign in now?',
    String? returnPath,
  }) {
    // 如果未提供返回路径，则使用当前完整路径包括查询参数
    final state = GoRouterState.of(context);
    // 保存当前路径作为返回路径，这是用户希望访问的页面
    final String path = returnPath ?? state.uri.toString();

    return showDialog<bool>(
      context: context,
      barrierDismissible: false,
      builder:
          (context) => LoginDialog(
            title: title,
            description: description,
            returnPath: path,
          ),
    );
  }
}
